package com.example.demo;


import org.apache.commons.lang3.StringUtils;

/**
 * description: Demo01 <br>
 * date: 2021/4/8 16:09 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class Demo01 {


    /**
     * 一个长字符串中含多个连续和不连续的空格，
     * 求不含空格的最长字符串长度，
     * 要求算法尽可能最优。例如输入："a1 123# abc"，结果：4；
     * @param args
     */
    public static void main(String[] args) {
        String str = "a1 123# abc";
        int tmp = 0;
        int max = 0;
        int start = 0;//头位置
        int end = 0; //尾空格位置
        max = getMaxLength(str, tmp, max, start, end);
        System.out.println("不含空格的最长字符串长度:"+max);
    }

    private static int getMaxLength(String str, int tmp, int max, int start, int end) {
        for (int i = 0; i < str.length(); i++) {


            if (str.charAt(i)==' '){
                end = i;
                tmp = end - start;
                if (start != 0){
                    tmp -=1;
                }
            }
            if (i == str.length()-1){
                end = i;
                tmp = end - start;
            }
            if (max == 0){
                max = tmp;
            }else {
                if (max < tmp){
                    tmp = max ^ tmp;
                    max = max ^ tmp;
                    tmp = max ^ tmp;
                }
            }
            start = end;

        }
        return max;
    }
}
